package com.atjg.autocat.utils;

import android.content.Context;
import android.util.Log;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;

/* loaded from: classes5.dex */
public class CacheUtil {
    public static File createFile(Context context, String fileName) throws IOException {
        context.getPackageName();
        String path = context.getCacheDir().getAbsolutePath() + File.separator + "net_disconnect";
        File dirs = new File(path);
        if (!dirs.exists()) {
            dirs.mkdirs();
        }
        File file = new File(dirs, fileName);
        try {
            if (!file.exists()) {
                file.createNewFile();
            }
            Log.e("TAG", "file=" + file.getAbsolutePath());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }

    public static File[] listFile(Context context) {
        context.getPackageName();
        String path = context.getCacheDir().getAbsolutePath() + File.separator + "net_disconnect";
        File dirs = new File(path);
        File[] listFiles = dirs.listFiles();
        return listFiles;
    }

    public static void deleteFile(Context context, String fileName) throws FileNotFoundException {
        String path = context.getCacheDir().getAbsolutePath() + File.separator + "net_disconnect";
        File dirs = new File(path);
        if (!dirs.exists()) {
            throw new FileNotFoundException("文件夹不存在");
        }
        File file = new File(dirs, fileName);
        if (!file.exists()) {
            throw new FileNotFoundException("文件不存在");
        }
        if (file.isFile()) {
            file.delete();
        }
        Log.e("TAG", "file=文件删除成功");
    }

    public static void saveDataToCache(Context context, String fileName, String jsonData) throws IOException {
        File file = createFile(context, fileName);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(jsonData.getBytes());
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e2) {
            e2.printStackTrace();
        }
    }

    public static String getJsonStr(Context context, String fileName) {
        String path = context.getCacheDir().getAbsolutePath() + File.separator + "net_disconnect";
        File dirs = new File(path);
        File file = new File(dirs, fileName);
        StringBuilder sb = new StringBuilder();
        try {
            BufferedReader reader = new BufferedReader(new FileReader(file));
            while (true) {
                String str = reader.readLine();
                if (str == null) {
                    break;
                }
                sb.append(str);
            }
            reader.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e2) {
            e2.printStackTrace();
        }
        return sb.toString();
    }
}